// https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
// 102. 二叉树的层序遍历
// 给定二叉树的根节点 root，返回其节点值的层序遍历。
import { TreeNode } from '../utils/index.ts';
function levelOrder(root: TreeNode | null): number[][] {
    const result: number[][] = [];
    if (root === null) return result;

    const queue: TreeNode[] = [root];
    while (queue.length > 0) {
        const currentSize = queue.length;
        const arr: number[] = [];
        for (let i = 0; i < currentSize; i++) {
            const node = queue.shift();
            if (node) {
                arr.push(node.val);
                if (node.left) queue.push(node.left);
                if (node.right) queue.push(node.right);
            }
        }
        result.push(arr);
    }

    return result;
}
